package com.shixi.shixitraining.shixitraining.ui.tabhome.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.AuthenDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.OverseasDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.TrainingDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.WebActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.HomeAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.BannerBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.BannerEntity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HomeBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HomeGrideBean;
import com.shixi.shixitraining.shixitraining.util.GlideImageLoader;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.HOST;

/**
 * Created by shenyan on 2018/7/5.
 */

public class HomeFragment extends TabhostBaseFragment {
    public Banner banner;
    private RecyclerView home_rcview;
    private String image;

    //private  int DISTANCE_WHEN_TO_SELECTED=40;
    //private  int distanceY=0;
   //   private View homeTitleBarBgView;
   //    private TextView mHomeTv;
    private List<String> urlData = new ArrayList<>();
    private List<Object> data = new ArrayList<>();
    private BannerEntity bannerData;
    private Context mContext;
    private List<String> adList;


    @Override
    protected int getlayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initPager() {
        initView();
        initBanner();
        initHomeList();
    }


    @Override
    public void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    private void initView() {

        home_rcview = (RecyclerView)mContentView.findViewById(R.id.rcview_home);
        banner = (Banner) mContentView.findViewById(R.id.banner);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        home_rcview.setLayoutManager(manager);

        initData();
    }

    private void initHomeList() {


        HttpUtil.getInstance().getApiInterface().homeList(HOST + "/index/index").enqueue(new CallbackImple<HomeBean>() {
            @Override
            public void onError(Call<HomeBean> call, Throwable t) {
             ToastUtil.showShortToast(mContext,t.getMessage());
            }

            @Override
            public void onSuccess(Call<HomeBean> call, HomeBean response) {
                int code = response.getStatus();
                if (response != null&&response.getData()!=null) {
                    if (code == 200) {
                        HomeBean.DataBean listData = response.getData();
                        List<HomeBean.DataBean.JobsBean> jobData = listData.getJobs();

                        List<HomeBean.DataBean.TrainBean> trainData = listData.getTrain();
                        List<HomeBean.DataBean.TrainingBean> traininData = listData.getTraining();
                        data.add(traininData);
                        data.add(jobData);
                        data.add(trainData);

                    } else {
                       ToastUtil.showShortToast(mContext,response.getMsg());
                    }


                } else {

               ToastUtil.showShortToast(mContext,"数据为空");
                }

            }
        });

    }

    private void initBanner() {

        HttpUtil.getInstance().getApiInterface().banner(HOST+"/index/ad").enqueue(new CallbackImple<BannerBean>() {

            private String url;

            @Override
            public void onError(Call<BannerBean> call, Throwable t) {
                ToastUtil.shortToast(t.getMessage());
            }

            @Override
            public void onSuccess(Call<BannerBean> call, BannerBean response) {
                adList = new ArrayList<>();
                if (response != null&&response.getData()!=null) {
                    if (response.getStatus() == 200) {
                        List<BannerEntity> adData = response.getData();
                        for (int i = 0; i < adData.size();i++) {
                            bannerData = adData.get(i);
                            String image = bannerData.getImage();
                            url = bannerData.getUrl();
                            String type = bannerData.getType();
                            String id = bannerData.getId();
                            adList.add(bannerData.getImage());
                        }
                        banner.setImages(adList).setImageLoader(new GlideImageLoader()).setIndicatorGravity(BannerConfig.CENTER).start();
                    } else {
                        ToastUtil.shortToast(response.getMsg());
                    }
                } else {
                    ToastUtil.shortToast("数据为空");
                }
            }
        });


        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                String title = bannerData.getTitle();
                String url = bannerData.getUrl();
                String type = adList.get(position);

                switch (type){
                    case "link":
                        gotoBrowser(title,url);
                        break;
                    case "training":
                        startActivity(new Intent(mContext, TrainingDetailActivity.class));
                        break;
                    case "train":
                        startActivity(new Intent(mContext, AuthenDetailActivity.class));
                        break;
                    case "job":
                        startActivity(new Intent(mContext, OverseasDetailActivity.class));
                        break;
                }

            }
        });

    }

    private void gotoBrowser(String title, String url) {

        Intent intent=new Intent(mContext, WebActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("title",title);
        startActivity(intent);
    }

    private void initData() {

//        homeTitleBarBgView = mContentView.findViewById(R.id.home_title_bar_bg_view);
//        mHomeTv = (TextView) mContentView.findViewById(R.id.tv_home);

        //gride
        List<HomeGrideBean> gride = new ArrayList<>();
        gride.add(new HomeGrideBean("实训项目", R.drawable.icon_program, 0));
        gride.add(new HomeGrideBean("海外就业", R.drawable.icon_overseas_job, 1));
        gride.add(new HomeGrideBean("培训项目", R.drawable.icon_authen, 2));
        data.add(gride);

        HomeAdapter adapter = new HomeAdapter(getContext(), data);
        home_rcview.setAdapter(adapter);

//        home_rcview.setOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                distanceY += dy;
//                if (distanceY > DensityUtil.dp2px(getContext(), 20)) {
//                    homeTitleBarBgView.setBackgroundColor(getResources().getColor(R.color.basepresscolor));
//                    mHomeTv.setVisibility(View.VISIBLE);
//                    if (Build.VERSION.SDK_INT > 21) {
//                        homeTitleBarBgView.setAlpha(distanceY * 1.0f / DensityUtil.dp2px(getContext(), 100));
//                    }
//                    else {
//                        DISTANCE_WHEN_TO_SELECTED = 20;
//                    }
//                }
//                else {
//                    homeTitleBarBgView.setBackgroundColor(0x0000000);
//                    mHomeTv.setVisibility(View.GONE);
//                }
//
//            }
//        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }


}
